package com.brillilab.service.core.method.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.method.MethodStepMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.enums.method.MethodSourceEnum;
import com.brillilab.domain.po.method.Method;
import com.brillilab.domain.po.method.MethodStep;
import com.brillilab.service.core.method.IMethodStepService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wuzhiyu
 * @Title: MethodStepServiceImpl
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/2/2210:34
 */
@Service
@Transactional
public class MethodStepServiceImpl implements IMethodStepService {
    @Resource
    private MethodStepMapper methodStepMapper;

    @Override
    public Long saveStepAndUpdateInitId(MethodStep step,Integer fromType) {
        methodStepMapper.insert(step);
        LambdaQueryWrapper<MethodStep> query = new LambdaQueryWrapper<>();
        query.eq(MethodStep::getId,step.getId());
        MethodStep ms = new MethodStep();
        ms.setId(step.getId());
        if(MethodSourceEnum.CREATE.getSourceType().equals(fromType)||step.getSourceStepId()==null){
            ms.setSourceStepId(step.getId());
        }
        ms.setInitStepId(step.getId());
        methodStepMapper.update(ms,query);
        return step.getId();
    }

    @Override
    public List<MethodStep> listByMethodId(Long methodId, BoolEnum isDelete) {
        LambdaQueryWrapper<MethodStep> query = new LambdaQueryWrapper<>();
        query.eq(MethodStep::getMethodId,methodId);
        if(isDelete!=null){
            query.eq(MethodStep::getIsDelete,isDelete.getValue());
        }
        query.orderByAsc(MethodStep::getIsDelete,MethodStep::getSort);
        return methodStepMapper.selectList(query);
    }

    @Override
    public boolean save(MethodStep step) {
        methodStepMapper.insert(step);
        if(step.getSourceStepId()==null){
            MethodStep up=new MethodStep();
            up.setSourceStepId(step.getId());
            up.setId(step.getId());
            methodStepMapper.updateById(up);
        }
        return true;
    }

    @Override
    public List<MethodStep> listByPhaseId(Long methodPhaseId, BoolEnum isDelete) {
        LambdaQueryWrapper<MethodStep> query = new LambdaQueryWrapper<>();
        query.eq(MethodStep::getMethodPhaseId,methodPhaseId);
        if(isDelete!=null){
            query.eq(MethodStep::getIsDelete,isDelete.getValue());
        }
        query.orderByAsc(MethodStep::getIsDelete,MethodStep::getSort);
        return methodStepMapper.selectList(query);
    }

    @Override
    public MethodStep selectById(Long methodStepId) {
        return methodStepMapper.selectById(methodStepId);
    }

    @Override
    public boolean updateById(MethodStep methodStep) {
        return methodStepMapper.updateById(methodStep) > 0;
    }
}
